import {
  setStorageToken,
  getStorageToken,
  removeStorageToken
} from '@/utils/storage'
import { Toast } from 'vant'
import { getUserInfo } from '@/api/user'

export default {
  namespaced: true,
  state: {
    token: getStorageToken(),
    userInfo: {}
  },
  getters: {},
  mutations: {
    setToken(state, payload) {
      state.token = payload
      setStorageToken(payload)
    },
    // 修改个人信息
    setUserInfo(state, payload) {
      state.userInfo = payload
    },
    removeToken(state) {
      state.token = {}
      removeStorageToken()
    }
  },
  actions: {
    async Action_getUserInfo(context) {
      try {
        const res = await getUserInfo()
        context.commit('setUserInfo', res.data)
      } catch {
        // 提示下
        Toast('获取用户信息失败')
      }
    }
  },
  modules: {}
}
